﻿using Newtonsoft.Json.Linq;
using Serilog;
using System;
using System.Reflection;

namespace ybx_caller.service
{
    public class YbCallService
    {
        public void call()
        {
            try
            {
                HealthManager.Instance.SetHealth(HealthState.Up);
            }
            // 这里替换成医保调用失败实际抛出的异常类型
            catch (Exception ex)
            {
                Log.Error(ex, "YbCallService.call()  went wrong");
                HealthManager.Instance.SetHealth(HealthState.Down);
            }
        }

        public static string call_data(String input)
        {        
            string BackData = "";
            string jybh = "";
            string opter = "";
            string opter_name = "";
            string insuplc = "";
            string medplc = "";
            string sign_no = "";

            try
            {
                JObject jsonObj = JObject.Parse(input);
                jybh = jsonObj["infno"].ToString();
                opter= jsonObj["opter"].ToString();
                opter_name = jsonObj["opter_name"].ToString();
                insuplc = jsonObj["insuplc_admdvs"].ToString();
                medplc = jsonObj["mdtrtarea_admvs"].ToString();
                sign_no = jsonObj["sign_no"].ToString();

                Log.Information("【" + jybh + "】 start--------------------------");
                Log.Information("【"+ jybh +"】"+ "原始输入:" + input);
            }
            catch (Exception exx)
            {
                jybh = "输入字符串格式不对";
                BackData = "输入字符串格式不对";
                Log.Information("【" + jybh + "】 start--------------------------");
                Log.Error("解析输入异常:" + exx.Message + "  【" + jybh + "】" + "原始输入:" + input, exx);
            }

            if ((jybh != "") && (jybh != "输入字符串格式不对"))
            {
                try
                {
                    var yhType = Type.GetTypeFromProgID("yinhai.yh_hb_sctr");
                    var yhObject = System.Activator.CreateInstance(yhType);
                    string astr_jysc = String.Empty;

                    if (sign_no == "111111111111111111111111111111" || sign_no == "")
                    {
                        string sign_no1 = CallYhYB.getSignNo(opter, opter_name, insuplc, medplc);
                        // 解析JSON字符串为JObject
                        JObject jsonObj = JObject.Parse(input);
                        // 替换sign_no节点的值
                        jsonObj["sign_no"] = sign_no1; // 将sign_no的值
                        // 将修改后的JObject转换回JSON字符串
                        input = jsonObj.ToString();
                        Log.Information("【" + jybh + "】" + "真实输入:" + input);
                    }
                    
                    var args = new object[] { jybh, input, astr_jysc };
                    var parameterModifier = new ParameterModifier(3);
                    parameterModifier[0] = false;
                    parameterModifier[1] = false;
                    parameterModifier[2] = true;
                    var parameterModifiers = new ParameterModifier[] { parameterModifier };

                    yhType.InvokeMember("yh_hb_call", BindingFlags.InvokeMethod, null, yhObject, args, parameterModifiers, System.Globalization.CultureInfo.CurrentCulture, null);

                    BackData = args[2].ToString();

                    yhType = null;
                    yhObject = null;

                    Log.Information("【" + jybh + "】交易输出:" + BackData);
                }
                catch (Exception ex)
                {
                    Log.Error("【" + jybh + "】交易调用出错:" + ex.Message, ex);
                }
            }
            Log.Information("【" + jybh + "】 end--------------------------");
            return BackData;
        }
    }
}
